using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace Tools
{
  public static class WebUtility
  {

    public static string GetStringFromUrl(string _Url, int _TimedOut, Encoding _PageEncode)
    {
      StreamReader strm = new StreamReader(GetStreamFromUrl(_Url, _TimedOut), _PageEncode);
      string result = "";
      result = strm.ReadToEnd();
      return result;
    }

    public static Stream GetStreamFromUrl(string _Url,int _TimedOut)
    {
      HttpWebResponse res;
      HttpWebRequest req;
      byte[] NullText = Encoding.ASCII.GetBytes("");
      Stream result = new MemoryStream(NullText);
      if (_Url != "")
      {
          try
          {
              req = (HttpWebRequest)WebRequest.Create(_Url);
              req.Method = "GET";
              req.Timeout = _TimedOut;
              res = (HttpWebResponse)req.GetResponse();

              if (res.StatusCode == HttpStatusCode.OK)
              {
                  result = res.GetResponseStream();
              }
              

          }
          catch(Exception e)
          {
              //throw;
            string tmpstr = e.Message;
          }
      
      }
      return result;    
    }
  }
}
